package controllers

import (
	"github.com/emicklei/go-restful/v3"
	"github.com/rs/zerolog/log"
	"webGoDemo/presenter"
	presenterKafka "webGoDemo/presenter/kafka"
	"webGoDemo/services/op"
	"webGoDemo/utils"
)

func Notify(request *restful.Request, response *restful.Response) {
	apiOutResult := presenter.NewCommonRes(response)
	defer func() {
		apiOutResult.Out()
	}()

	var updateClusterRequest presenterKafka.UpdateClusterRequest
	err := utils.ReadValidatableEntity(request, &updateClusterRequest)
	if err != nil {
		apiOutResult.Err = err
		apiOutResult.Code = presenter.CodeGetUpdateClusterReqFail
		apiOutResult.MsgRes = err.Error()
		apiOutResult.ToLogMsg["msg"] = "get update cluster from request failed"
		return
	}
}

func GetState(request *restful.Request, response *restful.Response) {

	apiOutResult := presenter.NewCommonRes(response)
	defer func() {
		apiOutResult.Out()
	}()
	instanceId := request.QueryParameter("instanceId")
	tenantId := request.QueryParameter("tenantId")
	log.Info().Str("instanceId", instanceId).Str("tenantId", tenantId)
	service := new(op.LogicOpService)
	state, err := service.CheckClusterState(instanceId, tenantId)

	if err != nil {
		apiOutResult.Err = err
		apiOutResult.MsgRes = presenter.MsgGetOpInstanceStateFail
		apiOutResult.ToLogMsg["operate-getInstanceState"] = err.Error()
		apiOutResult.Code = presenter.CodeGetOpInstanceStateFail
		return
	}
	// state 0 ,2  应该返回0 其余返回1
	if state == 0 || state == 2 {
		apiOutResult.Data = 0 // 正常状态
		apiOutResult.ToLogMsg["opReturnCode"] = state
	} else {
		apiOutResult.Data = 1 // 异常状态
		apiOutResult.ToLogMsg["opReturnCode"] = state
	}
}
